//
//  WPBaseSpecificCasesVC.m
//  wobingwoyi
//
//  Created by WGP on 16/8/16.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPBaseSpecificCasesVC.h"
#import "NSString+WPSizeOfString.h"
#import "WPBtnNoHighLight.h"

@interface WPBaseSpecificCasesVC ()

@property(nonatomic,strong)UIView *statusBarView;
@property(nonatomic,strong)UIButton *backBtn;
@property(nonatomic,assign)CGFloat margin;
@property(nonatomic,assign)CGSize fontSize;
@property(nonatomic,strong)WPBtnNoHighLight *selectedBtn;
@property(nonatomic,strong)UIView *sliderView;

@property(nonatomic,strong)UICollectionViewFlowLayout *layout;

@end

@implementation WPBaseSpecificCasesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

-(void)setupUI
{
    [self statusBarView];
    [self navBarView];
    [self titleLabel];
    [self backBtn];
    [self titleView];
//    [self tipImageView];
    [self collectionView];
    [self lineView];
}

#pragma mark - 懒加载
-(UIView *)statusBarView
{
    if (_statusBarView == nil)
    {
        _statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _statusBarView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_statusBarView];
        
    }
    return _statusBarView;
}

-(UIView *)navBarView
{
    if (_navBarView == nil)
    {
        _navBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
        _navBarView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_navBarView];
    }
    return  _navBarView;
}

-(UILabel *)titleLabel
{
    if (_titleLabel == nil)
    {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = GLobalFontColor;
        _titleLabel.font = [UIFont systemFontOfSize:18];
        [self.navBarView addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.navBarView);
        }];
        
    }
    return _titleLabel;
}



-(UIButton *)backBtn
{
    if (_backBtn == nil)
    {
        _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"back_normal"] forState:UIControlStateNormal];
        [_backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
        [_backBtn sizeToFit];
        [_backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [self.navBarView addSubview:_backBtn];
        
        [_backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.navBarView);
            make.left.equalTo(self.navBarView).offset(10);
        }];
        
        [self setupTitleView];
    }
    return _backBtn;
}

-(UIView *)titleView
{
    if (_titleView == nil)
    {
        _titleView = [[UIView alloc] init];
        _titleView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_titleView];
        
        [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.navBarView.mas_bottom);
            make.left.right.equalTo(self.view);
            make.height.mas_equalTo(44);
        }];
    }
    return _titleView;
}

-(void)setupTitleView
{
    UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, SCREEN_WIDTH, 0.5)];
    topLine.backgroundColor = GLobalLineColor;
    bottomLine.backgroundColor = GLobalLineColor;
    [self.titleView addSubview:topLine];
    [self.titleView addSubview:bottomLine];
    
    WPBtnNoHighLight *secondBtn = [self buttonWithTitle:@"治疗说明" withTag:2];
    WPBtnNoHighLight *firstBtn = [self buttonWithTitle:@"诊断凭据" withTag:1];
    
    self.selectedBtn = firstBtn;
    self.selectedBtn.selected = YES;
    
    WPBtnNoHighLight *thirdbtn = [self buttonWithTitle:@"用户评论" withTag:3];
    
    UIView *sliderView = [UIView new];
    sliderView.backgroundColor = GlobalColor;
    self.sliderView = sliderView;
    [self.titleView addSubview:sliderView];
    
    [secondBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.titleView);
    }];
    
    [firstBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(secondBtn);
        make.right.equalTo(secondBtn.mas_left).offset(-self.margin);
    }];
    
    [thirdbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(secondBtn);
        make.left.equalTo(secondBtn.mas_right).offset(self.margin);
    }];
    
    [sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.fontSize.width);
        make.height.mas_equalTo(2);
        make.centerY.equalTo(self.titleView.mas_bottom);
        make.left.equalTo(self.titleView).offset(self.margin);
    }];
    
    
}

-(UIImageView *)lineView
{
    if (_lineView == nil)
    {
        _lineView = [[UIImageView alloc] init];
        _lineView.backgroundColor = GLobalLineColor;
        [self.view addSubview:_lineView];
        
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0.5);
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.titleView.mas_bottom).offset(10);
        }];
    }
    return _lineView;
}

-(UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        [self.view addSubview:_collectionView];
        
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.view);
            make.top.equalTo(self.titleView.mas_bottom).offset(10);
        }];
        
        self.layout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-64-44-1);
        self.layout.minimumLineSpacing = 0;
        self.layout.minimumInteritemSpacing = 0;
        self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView.pagingEnabled = YES;
        _collectionView.bounces = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = GlobalBgColor;
    }
    return _collectionView;
}

-(WPBtnNoHighLight *)buttonWithTitle:(NSString *)title withTag:(NSInteger)tag
{
    WPBtnNoHighLight *btn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
    btn.tag = tag;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setTitleColor:GLobalFontColor forState:UIControlStateSelected];
    [btn setTitleColor:GloballightFontColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(titleBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    [self.titleView addSubview:btn];
    
    return btn;
}

-(CGFloat)margin
{
    if (_margin == 0)
    {
        NSString *str = @"我病我医";
        CGSize size = [str sizeWithMaxSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) font:15];
        self.fontSize = size;
        
        _margin = (SCREEN_WIDTH-3*size.width)/4;
    }
    return _margin;
}

-(void)titleBtnDidClick:(WPBtnNoHighLight *)btn
{
        if (self.selectedBtn == btn)
        {
            return ;
        }
    
        self.selectedBtn.selected = NO;
        self.selectedBtn = btn;
        self.selectedBtn.selected = YES;
    
//    NSLog(@"%@",NSStringFromCGRect(btn.frame));
    [self moveSliderWith:btn.tag];
}

-(void)moveSliderWith:(NSInteger)tag
{
    if (tag == 1)
    {
        [UIView animateWithDuration:0.3 animations:^{
            
            self.sliderView.transform = CGAffineTransformIdentity;
            
        }];
    }
    else if (tag == 2)
    {
        [UIView animateWithDuration:0.3 animations:^{
            
            self.sliderView.transform = CGAffineTransformMakeTranslation((self.margin+self.fontSize.width), 0);
            
        }];
    }
    else if (tag == 3)
    {
        [UIView animateWithDuration:0.3 animations:^{
            
            self.sliderView.transform = CGAffineTransformMakeTranslation(2*(self.margin+self.fontSize.width), 0);
            
        }];
    }
}

-(void)setIsExample:(BOOL)isExample
{
    _isExample = isExample;
    self.tipImageView.hidden = !isExample;
}

-(void)backBtnDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end



















